// @author caoshi
// @date 2023/2/14 10:07
// @since 1.0
package utils

import beego "github.com/beego/beego/v2/server/web"

type ResponseController struct {
	beego.Controller
}

type ResponseResult struct {
	Code int
	Message string
	Data interface{}
}

// 成功
func (r *ResponseController) Success(msg string, data interface{}) {
	if msg == "" {
		msg = "success"
	}
	res := ResponseResult{
		Code: 200,
		Message: msg,
		Data: data,
	}

	r.Data["json"] = res
	_ = r.ServeJSON()
	r.StopRun()
}

func (r *ResponseController) SuccessNonMsg(data interface{}) {
	r.Success("success", data)
}

// 失败
func (r *ResponseController) Error(msg string, data interface{}) {
	if msg == "" {
		msg = "error"
	}
	res := ResponseResult{
		Code: 500,
		Message: msg,
		Data: data,
	}
	r.Data["json"] = res
	_ = r.ServeJSON()
	r.StopRun()
}
